/**
 * 购物车
 * 2014-1-13
 */

CART = {};
CART.url = '';

jQuery().ready(function(){
	
});

//获取购物车内容
CART.getProductLoading = false;
CART.getProduct = function(){
	if(CART.getProductLoading){
		return false;
	}
	CART.getProductLoading = true;
	var url = CART.url + 'cart/Carts/getProduct';
	jQuery.ajax({
		type:'POST',url:url,
		data:{'operat_type':'mobile'},
		dataType:'json',
		success : function(txt){
			if(txt.state){
				jQuery('ul>li:jqmData(cart="cartlist")').replaceWith(txt.html);
			}else{
				//alert(txt.msg);
			}
		},
		error: function (data, status, e){
		},
		complete:function(){
			CART.getProductLoading = false;
		}
	});
};

//加入购物车
CART.addLoading = false;
CART.add = function(meal_weekly_id,number){
	meal_weekly_id = meal_weekly_id?meal_weekly_id:0;//餐品Id
	number = number?number:1;//数量
	
	if(CART.addLoading){
		return false;
	}
	CART.addLoading = true;
	var url = CART.url + 'cart/Carts/add';
	jQuery.ajax({
		type:'POST',url:url,
		data:{'operat_type':'mobile','meal_weekly_id':meal_weekly_id,'number':number},
		dataType:'json',
		success : function(txt){
			if(txt.state){
				GM.tip(txt.msg,true);
				jQuery('ul>li:jqmData(cart="cartlist")').replaceWith(txt.html);
			}else{
				GM.tip(txt.msg);
			}
		},
		error: function (data, status, e){
		},
		complete:function(){
			CART.addLoading = false;
		}
	});
};

//删除单个产品
CART.deleteProductLoading = false;
CART.deleteProduct = function(meal_weekly_id){
	meal_weekly_id = meal_weekly_id?meal_weekly_id:0;//餐品Id
	
	if(CART.deleteProductLoading){
		return false;
	}
	CART.deleteProductLoading = true;
	var url = CART.url + 'cart/Carts/deleteProduct';
	jQuery.ajax({
		type:'POST',url:url,
		data:{'meal_weekly_id':meal_weekly_id},
		dataType:'json',
		success : function(txt){
			if(txt.state){
				jQuery('tbody>tr:jqmData(mealweeklyid="'+ meal_weekly_id +'")').remove();//移除tr
				jQuery('tfoot>tr:jqmData(count="total")').find('span').html(txt.data.totalPrice);//总价
				jQuery('tfoot>tr:jqmData(count="total")').find('em').html(txt.data.totalPrice);//积分

				var obj = jQuery('tbody:jqmData(cart-tbody="product")>tr');
				if(obj.length <= 0){
					var tr = '<tr><td height="30" align="center" colspan="4">暂无记录</td></tr>';
					jQuery('tbody:jqmData(cart-tbody="product")').html(tr).trigger('create');
				}
			}else{
				alert(txt.msg);
			}
		},
		error: function (data, status, e){
		},
		complete:function(){
			CART.deleteProductLoading = false;
		}
	});
};

//减产品
CART.minusProductLoading = false;
CART.minusProduct = function(meal_weekly_id){
	meal_weekly_id = meal_weekly_id?meal_weekly_id:0;//餐品Id
	var number = jQuery('#number_'+meal_weekly_id).val();//数量
	
	/*if(!CART.stock(meal_weekly_id,number-1)){//判断库存
		return false;
	}*/
	number = parseInt(number)-1;
	if(number <= 0){//至少购买一份
		alert("至少购买一份");
		return false;
	}
	
	if(CART.minusProductLoading){
		return false;
	}
	CART.minusProductLoading = true;
	var url = CART.url + 'cart/Carts/minusProduct';
	jQuery.ajax({
		type:'POST',url:url,
		data:{'operat_type':'mobile','meal_weekly_id':meal_weekly_id},
		dataType:'json',
		success : function(txt){
			if(txt.state){
				jQuery('tbody>tr:jqmData(mealweeklyid="'+ meal_weekly_id +'")').replaceWith(txt.html);
				jQuery('tfoot>tr:jqmData(count="total")').find('span').html(txt.data.totalPrice);//总价
				jQuery('tfoot>tr:jqmData(count="total")').find('em').html(txt.data.totalPrice);//积分
			}else{
				alert(txt.msg);
			}
		},
		error: function (data, status, e){
		},
		complete:function(){
			CART.minusProductLoading = false;
		}
	});
};

//增加产品
CART.plusProductLoading = false;
CART.plusProduct = function(meal_weekly_id){
	meal_weekly_id = meal_weekly_id?meal_weekly_id:0;//餐品Id
	
	var number = jQuery('#number_'+meal_weekly_id).val();//数量
	/*if(!CART.stock(meal_weekly_id,number+1)){//判断库存
		return false;
	}*/
	
	if(CART.plusProductLoading){
		return false;
	}
	CART.plusProductLoading = true;
	var url = CART.url + 'cart/Carts/plusProduct';
	jQuery.ajax({
		type:'POST',url:url,
		data:{'operat_type':'mobile','meal_weekly_id':meal_weekly_id},
		dataType:'json',
		success : function(txt){
			if(txt.state){
				jQuery('tbody>tr:jqmData(mealweeklyid="'+ meal_weekly_id +'")').replaceWith(txt.html);
				jQuery('tfoot>tr:jqmData(count="total")').find('span').html(txt.data.totalPrice);//总价
				jQuery('tfoot>tr:jqmData(count="total")').find('em').html(txt.data.totalPrice);//积分
			}else{
				alert(txt.msg);
			}
		},
		error: function (data, status, e){
		},
		complete:function(){
			CART.plusProductLoading = false;
		}
	});
};

//产品数量
CART.updateProductLoading = false;
CART.updateProduct = function (meal_weekly_id){
	var number = jQuery('#number_'+meal_weekly_id).val();//数量
	/*if(!CART.stock(meal_weekly_id,number)){//判断库存
		return false;
	}*/
	
	if(CART.updateProductLoading){
		return false;
	}
	CART.updateProductLoading = true;
	
	var url = CART.url + 'cart/Carts/updateProduct';
	jQuery.ajax({
		type:'POST',url:url,
		data:{'operat_type':'mobile','meal_weekly_id':meal_weekly_id,'number':number},
		dataType:'json',
		success : function(txt){
			if(txt.state){
				jQuery('tbody>tr:jqmData(mealweeklyid="'+ meal_weekly_id +'")').replaceWith(txt.html);
				jQuery('tfoot>tr:jqmData(count="total")').find('span').html(txt.data.totalPrice);//总价
				jQuery('tfoot>tr:jqmData(count="total")').find('em').html(txt.data.totalPrice);//积分
			}else{
				alert(txt.msg);
			}
		},
		error: function (data, status, e){
		},
		complete:function(){
			CART.updateProductLoading = false;
		}
	});
};
